from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from django.conf import settings

import re

from .models import Order, User, Estimate
from .ser import OrderModelSerializer, UserModelSerializer, EstimateModelSerializer

class Login(APIView):
    def post(self,request):
        phone = request.data.get('phone')
        password = request.data.get('password')
        user = User.objects.get(phone=phone,password=password)
        phone_re = re.match('^(13|14|15|18)[0-9]{9}$', phone)
        if phone_re:
            return Response({'msg': '手机号格式不正确', 'code': 400})
        if user:
            return Response({'msg': '登录成功', 'code': 200,' user_id': user.id})
        else:
            return Response({'msg': '用户或密码不正确', 'code':400})
class OrderView(APIView):
    def get(self,request):
        user_id = request.query_params.get('id')
        order = Order.objects.filter(user=user_id)
        order_ser = OrderModelSerializer(order, many=True)
        return Response(order_ser.data)
class EstimateView(APIView):
    def post(self,request):
        data = request.data
        estimate = EstimateModelSerializer(data=data)
        estimate.is_valid()
        estimate.save()
        return Response({'msg':'发表成功', 'code': 200})






